﻿/*------------------------------------------------
coded by Tophy 2005/2/20
-------------------------------------------------*/
import Math2;
class ColorPick {
	//属性
	[Inspectable(defaultValue="true")]
	public var showHelp:Boolean
	public var me:Object;
	public var tag:MovieClip;
	private var obj:Object;
	private var colorArray:Array;
	private var help:String = "";
	//静态函数
	//构造函数及init
	public function ColorPick(_help:Boolean) {
		if (_help) {
		} else {
			_help = false;
		}
		init(_help);
	}
	public function init(_help:Boolean) {
		help = "---- 简单取色器v1.0 ----\r";
		help += "coded by Tophy 2005/2/20\r";
		help += "QQ:40730122\r";
		help += "---- 说      明 ----\r";
		help += "实例.show(显示目标,回调函数或其它)\r";
		help += "    其中的回调函数参数为 sender,color\r";
		help += "    sender为colorPicker实例,color为颜色代码(6位，如：\"FF0000\")\r";
		help += "    如果回调参数不是函数则：\r";
		help += "        number：color的十进制数值。\r";
		help += "        string：color的颜色代码。\r";
		help += "        object：将创建属性colorSt和sender,其中colorSt为颜色代码,sender为colorPicker实例\r";
		help += "    如果回调参数为其它，则会将颜色代码复制到剪切板中\r";
		help += "实例.close()\r";
		help += "    关闭实例。\r";
		help += "---------------------\r";
		if (_help||showHelp) {
			trace(help);
		}
		me = this;
		colorArray = new Array();
		initColorArray();
	}
	//功能函数
	private function initColorArray() {
		//------------- b
		for (var b = 0; b<256; b += 34) {
			//----------- R
			var ca:Array = new Array();
			var bb:String = Math2.toX(b, 16);
			if (bb.length<2) {
				bb = "0"+bb;
			}
			for (var r = 0; r<256; r += 34) {
				//------------  g
				var rr:String = Math2.toX(r, 16);
				if (rr.length<2) {
					rr = "0"+rr;
				}
				for (var g = 0; g<256; g += 34) {
					var gg:String = Math2.toX(g, 16);
					if (gg.length<2) {
						gg = "0"+gg;
					}
					var c = rr+gg+bb;
					ca.push(c);
					//trace(c);
				}
				//end g
			}
			//end r
			colorArray.push(ca);
		}
		//end b
	}
	//-------------------------------------------------------  显示面板
	public function show(_tag:MovieClip, _obj:Object) {
		if (_tag != undefined) {
			tag = _tag;
			var d:Number = tag.getNextHighestDepth();
			if (d>1048570) {
				d = 1048571;
			}
			tag.createEmptyMovieClip("myColorPicker", ((d>=0) ? d : 0));
			tag = tag.myColorPicker;
		}
		if (tag == undefined) {
			trace("没有面板的生成位置!!");
			return;
		}
		obj = _obj;
		createBJ();
		createColor();
	}
	//-------------------------------------------------------  关闭面板
	public function close() {
		tag.BJ.removeMovieClip();
		tag.color.removeMovieClip();
		tag.MyColor.removeMovieClip();
		tag.closeBox.removeMovieClip();
		tag.removeMovieClip();
		trace(tag);
	}
	private function createBJ() {
		tag.createEmptyMovieClip("BJ", 10);
		tag.BJ.beginFill(0xffffff, 100);
		tag.BJ.lineStyle(0, 0x000000, 100);
		tag.BJ.moveTo(0, 0);
		tag.BJ.lineTo(200, 0);
		tag.BJ.lineTo(200, 100);
		tag.BJ.lineTo(0, 100);
		tag.BJ.lineTo(0, 0);
		tag.BJ.endFill();
	}
	private function createColor() {
		var w:Number = 10;
		var h:Number = 10;
		tag.createEmptyMovieClip("color", 20);
		tag.createEmptyMovieClip("MyColor", 30);
		tag.createEmptyMovieClip("closeBox",40);
		tag.closeBox.me=me;
		tag.closeBox.onRelease=function(){
			this.close();
		};
		createBox(tag.closeBox, "0x000000", 18, 18);
		tag.closeBox.lineStyle(1,0xffffff,100);
		tag.closeBox.moveTo(1,1);
		tag.closeBox.lineTo(17,17);
		tag.closeBox.moveTo(17,1);
		tag.closeBox.lineTo(1,17);
		
		var co:MovieClip = tag.color;
		var MyCo:MovieClip = tag.MyColor;
		createBox(MyCo, "0xFFFFFF", 106, 18);
		tag.createTextField("Label", 31, 1, 1, 100, 100);
		tag.Label.text = "自定义的颜色";
		tag.Label.autoSize = true;
		MyCo._x = tag.Label._x+tag.Label.textWidth+2;
		MyCo._y = 1;
		tag.closeBox._x=MyCo._x+MyCo._width+1;
		tag.closeBox._y=1;
		for (var i = 0; i<colorArray.length; i++) {
			for (var l = 0; l<colorArray[i].length; l++) {
				co.createEmptyMovieClip("sk"+colorArray[i][l], co.getNextHighestDepth());
				var nowSk:MovieClip = co["sk"+colorArray[i][l]];
				createBox(nowSk, "0x"+colorArray[i][l], w, h);
				if (l>=colorArray[i].length/2) {
					nowSk._x = (l-colorArray[i].length/2)*w;
					nowSk._y = (i+colorArray.length)*h;
				} else {
					nowSk._x = l*w;
					nowSk._y = i*h;
				}
				nowSk.color = colorArray[i][l];
				nowSk.conColor = new Color(MyCo);
				nowSk.root = me;
				nowSk.onRollOver = function() {
					this.conColor.setRGB("0x"+this.color);
				};
				nowSk.onRelease = function() {
					switch (typeof (this.root.obj)) {
					case "number" :
						return parseInt(this.color);
						break;
					case "string" :
						return this.color;
						break;
					case "function" :
						var f:Function = this.root.obj;
						f(this.root, this.color);
						break;
					case "object" :
						this.root.obj.colorSt = this.color;
						this.root.obj.sender = this.root;
						break;
					default :
						System.setClipboard(this.color);
						break;
					}
				};
			}
		}
		co._width = 198;
		co._height = 79;
		co._x = 1;
		co._y = 20;
	}
	private function createBox(obj:MovieClip, color, w:Number, h:Number) {
		obj.beginFill(color, 100);
		obj.moveTo(0, 0);
		obj.lineTo(w, 0);
		obj.lineTo(w, h);
		obj.lineTo(0, h);
		obj.lineTo(0, 0);
		obj.endFill();
	}
}
